package com.lujianfei.powersearch.ui.search

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseActivity
import com.lujianfei.powersearch.database.SearchItemModel
import com.lujianfei.powersearch.databinding.ActivitySearchBinding
import com.lujianfei.powersearch.ui.createOrUpdate.CreateOrUpdateActivity
import com.lujianfei.powersearch.ui.report.TotalConsumeReportActivity
import com.lujianfei.powersearch.ui.search.viewmodel.SearchActivityViewModel
import com.lujianfei.powersearch.ui.select.SelectNodeActivity
import com.lujianfei.powersearch.ui.subitem.SubItemActivity
import com.lujianfei.powersearch.utils.Constant
import com.lujianfei.powersearch.utils.LogUtils
import com.lujianfei.powersearch.utils.SystemUtils

class SearchActivity : BaseActivity<ActivitySearchBinding>(){

    companion object {

        private const val REQUEST_CODE = 100
        private const val TAG = "SearchActivity"

        fun jumpSearchActivity(context:Context) {
            val intent = Intent(context, SearchActivity::class.java)
            intent.putExtra("parentId", Constant.RootId)
            context.startActivity(intent)
        }

        fun jumpSearchActivity(context: Context, parentId: Long) {
            val intent = Intent(context, SearchActivity::class.java)
            intent.putExtra("parentId", parentId)
            context.startActivity(intent)
        }
    }
    var parentId: Long = 0
    override fun resourceId() = R.layout.activity_search

    private val mSearchActivityViewModel: SearchActivityViewModel by lazy { buildViewModel() }

    override fun initView() {
        binding?.vm = mSearchActivityViewModel
        setTitleMiddleText("搜索")
        binding?.searchBar?.showKeyboard()
    }
    override fun initEvent() {
        binding?.searchBar?.onSearchBarListener = { searchText ->
            mSearchActivityViewModel.search(searchText.trim(), parentId)
        }
        mSearchActivityViewModel.onItemClickCallback = { item ->
            jumpSubItemActivity(item)
        }
        mSearchActivityViewModel.onItemLongClickCallback = { model->
            val items = arrayOf("复制", "编辑", "生成报表", "取消")
            AlertDialog.Builder(this@SearchActivity).setTitle("提示").setItems(items) { dialog, which ->
                dialog.dismiss()
                val item = items[which]
                if ("复制".equals(item, ignoreCase = true)) {
                    SystemUtils.copy(model.name)
                } else if ("编辑".equals(item, ignoreCase = true)) {
                    CreateOrUpdateActivity.jumpCreateOrUpdateActivity(this, model, requestCode = REQUEST_CODE)
                } else if ("生成报表".equals(item, ignoreCase = true)) {
                    TotalConsumeReportActivity.start(this, model)
                }
            }.show()
            true
        }
    }

    override fun initData() {
        parseIntent()
    }

    private fun jumpSubItemActivity(item: SearchItemModel?) {
        val intent = Intent(this, SubItemActivity::class.java)
        intent.putExtra("data", item)
        startActivity(intent)
    }

    private fun parseIntent() {
        val intent = intent
        parentId = intent.getLongExtra("parentId", Constant.RootId.toLong())
    }

    override val titleBarResId = R.id.toolbar

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK) {
            return
        }
        if (requestCode == REQUEST_CODE) {
            mSearchActivityViewModel.reloadItem(
                data?.getLongExtra(
                    CreateOrUpdateActivity.PARAM_KEY_ID,
                    -1L
                ) ?: -1L
            )
        }
    }
}